﻿using MyClassLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyProgram
{
    public partial class FrmExamSchedule : Form
    {
        public FrmExamSchedule()
        {
            InitializeComponent();
        }
        global::MyDB.MedicareDataClassesDataContext DB = new MyDB.MedicareDataClassesDataContext();
        private void Form1_Load(object sender, EventArgs e)
        {
            toolStripComboBox1.SelectedIndex = 0;
            dataGridView1.DataSource = ClsExamSchedule.getSchedule();
            this.KeyPreview = true;                    
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            FrmExamScheduleNew f = new FrmExamScheduleNew();

            if (f.ShowDialog() == DialogResult.Yes)
            {
                dataGridView1.DataSource = ClsExamSchedule.getSchedule();
                int index = dataGridView1.Rows.Count - 1;
                dataGridView1.Rows[index].Selected = true;
            }

            ClsRecord.Record(sender, this.Name);
        }

        private void btn_Seach_Click(object sender, EventArgs e)
        {
            
            if (toolStripComboBox1.Text=="姓名")
            {
                var q = from p in DB.View_ExamSchedules
                        where p.姓名.Contains(toolStripTextBox2.Text)
                        select p;

                dataGridView1.DataSource = q.ToList();
            }
            else
            {
                var q = from p in DB.View_ExamSchedules
                        where p.病歷號碼.ToString() == toolStripTextBox2.Text
                        select p;

                dataGridView1.DataSource = q.ToList();
            } 


        }

        Random randomNum = new Random();
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            
            int pID = (from g in DB.Patients select g.病患ID).Max();; // Properties.Settings.Default.掛號ID % DB.Patients.Count() + 1;
           
            int random = randomNum.Next(1, 4);

            DB.RegisterInformations.InsertOnSubmit(new MyDB.RegisterInformation {病患ID = pID, 診次ID = random, 醫療人員ID = 4, });  //掛號資料ID = rID,
            DB.SubmitChanges();
            int rID = (from j in DB.RegisterInformations select j.掛號資料ID).Max();//Properties.Settings.Default.掛號ID;
            DB.Diagnosis.InsertOnSubmit(new MyDB.Diagnosi { 掛號資料ID = rID, 提醒天數ID = random, 診斷 = "骨質疏鬆", 護理師ID = 2, 諮詢師ID = 3, 醫生ID = 4 });
            DB.SubmitChanges();
            int dID = (from k in DB.Diagnosis select k.醫囑資料ID).Max();
            DB.ExamSchedules.InsertOnSubmit(new MyDB.ExamSchedule { 醫囑資料ID= dID, 檢驗項目ID = random, 檢驗頻率ID = random, 起始日期 = DateTime.Now.AddDays(randomNum.Next(-99,0)), 結束日期 = DateTime.Now.AddDays(randomNum.Next(0,99)), 備註 = "test", 結案原因 = "尚未結案", 結案=false });
            DB.SubmitChanges();

            var q = from p in DB.Patients
                    where p.病患ID == pID
                    select p.姓名;
            string[] x = { "" };
            foreach (var a in q)
            {
                x[0] = a;
            }
            MessageBox.Show(string.Format("新增了一筆就診資料 \n掛號ID : {0} \n病患姓名 : {1}", rID, x[0]));//Properties.Settings.Default.掛號ID,
           // Properties.Settings.Default.掛號ID += 1;
           // Properties.Settings.Default.Save();
            dataGridView1.DataSource = MyClassLibrary.ClsExamSchedule.getSchedule();

        }

        private void btn_Delete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("確定要刪除嗎?\n刪除後將無法復原!!", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
            == DialogResult.Yes)
            {
                int id = (int)(dataGridView1.SelectedRows[0].Cells[0].Value);
                var q = from p in DB.ExamSchedules
                        where p.排程ID == id
                        select p;

                DB.ExamSchedules.DeleteOnSubmit(q.FirstOrDefault());
                DB.SubmitChanges();
                dataGridView1.DataSource = MyClassLibrary.ClsExamSchedule.getSchedule();
            }
        }

        private void FrmExamScheduleManage_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode== Keys.Enter)
            {
                btn_Seach_Click(sender, e);
            }
        }

        private void btn_Edit_Click(object sender, EventArgs e)
        {
            int index = dataGridView1.SelectedRows[0].Index;
            ClsExamSchedule.id = (int)dataGridView1.SelectedRows[0].Cells[0].Value;
            
            FrmExamEdit f = new FrmExamEdit();
            f.ShowDialog();

            btn_Seach_Click(sender, e);
            dataGridView1.Rows[index].Selected = true;

            //ClsRecord.Record(sender, this.Name, DB.ExamSchedules.Where(n => n.排程ID==ClsExamSchedule.id).First().Diagnosis.RegisterInformation.Patients.病患ID);
        }

    }
}
